<?php
declare (strict_types=1); // php的严格模式，弱类型

namespace app\admin\controller;

use app\model\CommentModel;
use think\facade\Request;
use think\facade\Validate;
use think\facade\View;
use think\response\Json;

/**
 * 写功能的步骤：
 * 1. 视图先渲染出来，确定需要什么数据
 * 2. 获取参数，校验参数（可能没有）
 * 3. 使用Model处理（查询、写操作）
 * 4. 数据层model处理完成之后，把结果传递视图层view显示。
 *
 * Class Category
 * @package app\admin\controller
 */
class Comment
{
    // 评论列表
    public function index()
    {
        $status = Request::param('status',1);

        // 查找分类数据
        $commentList = CommentModel::where('status','=',$status)->select();

        View::assign('status',$status);
        View::assign('commentList', $commentList);
        return View::fetch();
    }

    public function garbage()
    {
        $comment_id = Request::param('comment_id');
        $validate = Validate::rule([
           'comment_id|评论id' => 'require|between:1,'.PHP_INT_MAX,
        ]);

        if(!$validate->check(['comment_id' => $comment_id])){
            $data = [
                'status' => 0,
                'message' => $validate->getError(),
                'data' =>[],
            ];
            return json($data);
        }
        $result = CommentModel::update(['status' => 2,'comment_id' => $comment_id]);

        if($result){
            $data = [
                'status' => 1,
                'message' => '',
                'data' =>[
                    'success'=> '移入回收站',
                ]
            ];
            return json($data);

        }
    }
    public function resume()
    {
        $comment_id = Request::param('comment_id');
        $validate = Validate::rule([
            'comment_id|评论id' => 'require|between:1,'.PHP_INT_MAX,
        ]);

        if(!$validate->check(['comment_id' => $comment_id])){
            $data = [
                'status' => 0,
                'message' => $validate->getError(),
                'data' =>[],
            ];
            return json($data);
        }
        $result = CommentModel::update(['status' => 1,'comment_id' => $comment_id]);

        if($result){
            $data = [
                'status' => 2,
                'message' => '',
                'data' =>[
                    'success'=> '恢复成功',
                ]
            ];
            return json($data);

        }
    }
    public function delete()
    {
        $comment_id = Request::param('comment_id');
        $validate = Validate::rule([
            'comment_id|评论id' => 'require|between:1,'.PHP_INT_MAX,
        ]);

        if(!$validate->check(['comment_id' => $comment_id])){
            $data = [
                'status' => 0,
                'message' => $validate->getError(),
                'data' =>[],
            ];
            return json($data);
        }
        $result = CommentModel::destroy($comment_id);

        if($result){
            $data = [
                'status' => 3,
                'message' => '',
                'data' =>[
                    'success'=> '彻底删除',
                ]
            ];
            return json($data);

        }
    }

}
